var icons = {google:"google",amazon:"amazon",facebook:"facebook-official",apple:"apple",microsoft:"windows",cloudflare:"cloud"}
browser.storage.local.get(["excludedDomains","siteExceptions"]).then(function(data) {
if(!data.excludedDomains) data.excludedDomains = [];
if(!data.siteExceptions) data.siteExceptions = {};
console.log(data);
for(var i=0;i<data.excludedDomains.length;i++) {
    const excludedDomainDiv = document.createElement('div');
    const excludedDomainKbd = document.createElement('kbd');

    document.getElementById('ignorelist').appendChild(excludedDomainDiv);

    const trashButton = document.createElement('button');
    trashButton.classList.add('btn', 'btn-sm', 'btn-outline-danger');

    const trashIcon = document.createElement('i');
    trashIcon.classList.add('fa', 'fa-trash');
    trashIcon.addEventListener('click', function () {
browser.runtime.sendMessage({
type:"removeFromExcludes",
subtype:"fromRules",
            domainname: data.excludedDomains[i],
},resp => {
if(resp === "Removed from Excludes") {
            excludedDomainDiv.parentNode.removeChild(excludedDomainDiv);
}
});
    });

    trashButton.appendChild(trashIcon);
    excludedDomainDiv.appendChild(trashButton);

    excludedDomainDiv.appendChild(excludedDomainKbd);
    excludedDomainDiv.dataset.domain = data.excludedDomains[i];
    excludedDomainKbd.textContent = data.excludedDomains[i];
}
for(var key in data.siteExceptions) {
    const exemptedDomainDiv = document.createElement('div');
    const exemptedDomainKbd = document.createElement('kbd');

    document.getElementById('rulelist').appendChild(exemptedDomainDiv);

    const trashButton = document.createElement('button');
    trashButton.classList.add('btn', 'btn-sm', 'btn-outline-danger');

    const trashIcon = document.createElement('i');
    trashIcon.classList.add('fa', 'fa-trash');

    trashButton.appendChild(trashIcon);
    exemptedDomainDiv.appendChild(trashButton);

for(var cloud in data.siteExceptions[key]) {
if(data.siteExceptions[key][cloud]) var buttonClass = "btn-primary";
else var buttonClass = "btn-outline-primary";
        const cloudButton = document.createElement('button');
        cloudButton.dataset.cloud = cloud;
        cloudButton.classList.add('btn', 'btn-sm', buttonClass);

        const cloudIcon = document.createElement('i');
        cloudIcon.classList.add('fa', 'fa-' + icons[cloud]);
        cloudIcon.addEventListener('click', function () {
browser.runtime.sendMessage({
type:"toggleSetting",
subtype:"fromRules",
                domainname: exemptedDomainDiv.dataset.domain,
                company: cloudButton.dataset.cloud,
},resp => {
console.log(this);
                cloudButton.classList.toggle('btn-primary');
                cloudButton.classList.toggle('btn-outline-primary');
});
});

}
    cloudButton.appendChild(cloudIcon);
    exemptedDomainDiv.appendChild(cloudButton);

    exemptedDomainDiv.appendChild(exemptedDomainKbd);
    exemptedDomainDiv.dataset.domain = key;
    exemptedDomainKbd.textContent = key;

    trashIcon.addEventListener('click', function () {
browser.runtime.sendMessage({
type:"toggleSetting",
subtype:"fromRules",
            domainname: key,
command:"resetDefault"
},resp => {
if(resp === "Removed Exception") {
            exemptedDomainDiv.parentNode.removeChild(exemptedDomainDiv);
}
});
    });
}
});
